<?PHP
require('paymentPlugin.php');
class pay_boc extends paymentPlugin{
    var $name="中国银行";
    var $logo="bank_boc";
    var $version=20081030;
    var $charset="utf-8";
	//var $intro = "中国银行";
    var $submitUrl="https://125.35.4.200:443/PGWPortal/RecvOrder.do";		//测试提交地址
	//var $submitUrl="https://ebs.boc.cn:441/PGWPortal/RecvOrder.do";			//正式提交地址
    var $submitButton = 'http://img.alipay.com/pimg/button_alipaybutton_o_a.gif'; ##需要完善的地方
    var $supportCurrency=array("CNY"=>001);
    var $supportArea =  array("AREA_CNY");
    //var $method = "post";
    var $head_charset = "utf-8";

    function toSubmit($payment){
		//error_log(print_r(func_get_args(),true),3,__FILE__.'.'.date('Y-m-d H-i-s').'.func_get_args.log');	//alone

        $merId = $this->getConf($payment['M_OrderId'], 'member_id');	//帐号
        //$ikey = $this->getConf($payment['M_OrderId'], 'PrivateKey');	//密钥
        $kpass = $this->getConf($paypment['M_OrderId'],'keyPass');		//密钥文件保护密码
        $keyFile = $this->getConf($paypment['M_OrderId'],'keyFile');	//私钥文件名
        $certFile = $this->getConf($paypment['M_OrderId'],'certFile');	//公钥文件名

		$money = number_format($payment['M_Amount'],2,'.','');		//精确到小数点后2位
		$remark = $payment['M_Remark'];
        $return['merchantNo'] = $merId;					//商户号
		$return['payType'] = '1';						//支付类型 1-网上购物 2-基金直销
        $return['orderNo'] = $payment['M_OrderId'];		//订单号
		$return['curCode'] = '001';						//目前只支持人民币-001
        $return['orderAmount'] = $money;	//订单金额
		$return['orderTime'] = date('YmdHis',$payment['M_Time']?$payment['M_Time']:time());			//订单时间
        //$return['orderNote'] = ($payment['M_Remark']?substr($payment['M_Remark'],0,15):"订单:{$payment['M_OrderNO']}");	//订单说明
		$return['orderNote'] = "订单:".$payment['M_OrderNO'];	//订单说明
        $return['orderUrl'] = $this->callbackUrl;		//支付完成返回地址

		if (file_exists(dirname(__FILE__)."/../../home/upload/boc/".$keyFile))
            $keyFile =dirname(__FILE__)."/../../home/upload/boc/".$keyFile;
        elseif (file_exists(dirname(__FILE__)."/../../cert/boc/".$keyFile))
            $keyFile =dirname(__FILE__)."/../../cert/boc/".$keyFile;
        if (file_exists(dirname(__FILE__)."/../../home/upload/boc/".$certFile))
            $certFile=dirname(__FILE__)."/../../home/upload/boc/".$certFile;
        elseif (file_exists(dirname(__FILE__)."/../../cert/boc/".$certFile))
            $certFile=dirname(__FILE__)."/../../cert/boc/".$certFile;

        $signStr = $payment['M_OrderId'].'|'.$return['orderTime'].'|'.$return['curCode'].'|'.$return['orderAmount'].'|'.$merId;
		$sign_file = dirname(__FILE__)."/../../home/upload/boc/sign";
		file_put_contents($sign_file,$signStr) or exit("无法写入数据,请检查".dirname($sign_file)."权限");

		if (is_file($sign_file))
		{
			if (strtoupper(substr(PHP_OS,0,3)) == 'WIN')
			{
				$PKCS7Tool = dirname(__FILE__)."/../../home/upload/boc/Sign.jar";
			}
			else
			{
				$PKCS7Tool = dirname(__FILE__)."/../../home/upload/boc/Sign.jar";
			}
			$cmd = "java -jar \"{$PKCS7Tool}\" \"{$keyFile}\" \"{$kpass}\" \"{$sign_file}\"";
			//error_log(print_r($cmd,true),3,__FILE__.'.cmd.log');	//alone
		}
		else
		{
			exit("网关签名数据参数错误");
		}

		if ($handle = popen($cmd,'r'))
		{
			while (!feof($handle))
			{
				$signData .= fgets($handle,512);
			}
			pclose($handle);
		}
		else
		{
			exit("无法执行数据签名!");
		}

        $return['signData'] = $signData;    //数据签名
        return $return;
    }
    function callback($in,&$paymentId,&$money,&$message){
		error_log(print_r(func_get_args(),true),3,__FILE__.'.'.date('Y-m-d H-i-s').'.callback.log');	//alone

        $merchantNo = $in['merchantNo'];	//帐号
        $orderNo = $in["orderNo"];			//订单号
		$paymentId = $in["orderNo"];		//商店支付号
		$orderSeq = $in['orderSeq'];		//订单流水号-唯一
		$cardTyp = $in['cardTyp'];			//银行卡类别
		$payTime = $in['payTime'];			//支付时间
		$orderStatus = $in['orderStatus'];	//订单状态 0-未处理 1-支付
        $money = $in["payAmount"];			//支付金额
		$signData = $in['signData'];		//网关签名数据
        $signStr = $merchantNo.'|'.$orderNo.'|'.$orderSeq.'|'.$cardTyp.'|'.$payTime.'|'.$orderStatus.'|'.$money;	//明文

		$kfile=$this->getConf($paymentId,'keyFile');
        $cfile=$this->getConf($paymentId,'certFile');
        $kPass=$this->getConf($paymentId,'keyPass');
        if (file_exists(dirname(__FILE__)."/../../home/upload/boc/".$kfile))
            $keyFile =dirname(__FILE__)."/../../home/upload/boc/".$kfile;
        elseif (file_exists(dirname(__FILE__)."/../../cert/boc/".$kfile))
            $keyFile =dirname(__FILE__)."/../../cert/boc/".$kfile;
        if (file_exists(dirname(__FILE__)."/../../home/upload/boc/".$cfile))
            $certFile=dirname(__FILE__)."/../../home/upload/boc/".$cfile;
        elseif (file_exists(dirname(__FILE__)."/../../cert/boc/".$cfile))
            $certFile=dirname(__FILE__)."/../../cert/boc/".$cfile;
		error_log(print_r($certFile,true),3,__FILE__.'.'.date('Y-m-d H-i-s').'.certFile.log');	//alone

		$verify_file = dirname(__FILE__)."/../../home/upload/boc/verify";
		$verify_sign_file = dirname(__FILE__)."/../../home/upload/boc/verifySign";
		file_put_contents($verify_file,$signStr);		//明文数据
		file_put_contents($verify_sign_file,$signData);		//支付网关返回的签名数据

		if (is_file($verify_file) && is_file($verify_sign_file))
		{
			if (strtoupper(substr(PHP_OS,0,3)) == 'WIN')
			{
				$PKCS7Tool = dirname(__FILE__)."/../../home/upload/boc/Verify.jar";
			}
			else
			{
				$PKCS7Tool = dirname(__FILE__)."/../../home/upload/boc/Verify.jar";
			}

			$cmd = "java -jar \"{$PKCS7Tool}\" \"{$certFile}\" \"{$verify_sign_file}\" \"{$verify_file}\"";
			error_log(print_r($cmd,true),3,__FILE__.'.'.date('Y-m-d H-i-s').'.cmd.log');	//alone
		}
		else
		{
			exit("网关签名数据参数错误");
		}

		if ($handle = popen($cmd,'r'))
		{
			while (!feof($handle))
			{
				$result .= fread($handle,512);
			}
			pclose($handle);
		}
		else
		{
			exit("无法执行签名验证!");
		}
		error_log(print_r($result,true),3,__FILE__.'.'.date('Y-m-d H-i-s').'.result.log');	//alone
        if (strpos($result,"VERIFY OK") === false){
			$message = '验证失败';
			error_log(print_r($message,true),3,__FILE__.'.'.date('Y-m-d H-i-s').'.if.log');	//alone
            return PAY_ERROR;
        }else{
            if ($orderStatus == 1){
				error_log(print_r('11',true),3,__FILE__.'.'.date('Y-m-d H-i-s').'.el.if.log');	//alone
                return PAY_SUCCESS;
            }else{
				$message = '交易失败';
				error_log(print_r($message,true),3,__FILE__.'.'.date('Y-m-d H-i-s').'.el.el.log');	//alone
                return PAY_FAILED;
            }
        }
    }

    function getfields(){
        return array(
            'member_id'=>array(
                'label'=>'客户号',
                'type'=>'string'
            ),
                        /*
                    'PrivateKey'=>array(
                            'label'=>'私钥',
                            'type'=>'string'
                        ), */
            'keyFile'=>array(
                'label'=>'私钥文件',
                'type'=>'file'
            ),
            'certFile'=>array(
                'label'=>'公钥文件',
                'type'=>'file'
            ),
            'keyPass'=>array(
                'label'=>'私钥保护密码',
                'type'=>'string'
            )
        );
    }
}
?>
